
****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all 
cap log close
set more off

use "CMS_Clean_Long.dta" , clear
merge m:1 appch1_link year using app1choices.dta, gen(app1merge)
keep if (app1merge==1 | app1merge==3)

merge m:1 mastid using frl_mb3.dta , gen(frl_mb3merge)    
keep if (frl_mb3merge==1 | frl_mb3merge==3)    
   
gen testgrade = grade  
keep if grade!=.
gen mbyear = year 

merge 1:1 mastid testgrade mbyear using nc_chars.dta , gen(mbmerge)    
keep if (mbmerge==1 | mbmerge==3)  
gen exit = (mbmerge==1)  
xtset mastid grade 

replace sciencestand=0 if year<2008
replace sci_grlevel=1 if year<2008

gen math_lag = L.mathstand 
gen read_lag = L.readstand 
gen science_lag = L.sciencestand 
gen frlmb_lag = L.frl_mb
gen lep_Y_lag = L.lep_Y
gen math_gr_lag = L.math_grlevel
gen read_gr_lag = L.read_grlevel
gen science_gr_lag = L.sci_grlevel
gen mardgr_int_lag = math_gr_lag*read_gr_lag
gen allgr_int_lag = math_gr_lag*read_gr_lag*science_gr_lag
gen ma_aig_lag = L.ma_aig
gen rd_aig_lag = L.rd_aig
gen aig_int_lag = rd_aig_lag*ma_aig_lag
keep if (grade>=0 & grade<=8 & homeschl!=. & appch1school!=. & homeschl!=appch1school & appch1!=0 & placementtype!="S")

forv g=0/8 {
su frpl if grade==`g' 
replace frpl = `r(mean)' if (grade==`g' & year>2010)
	}

foreach y in 07 08 09 10 11 12 13 {
	gen frpl`y' = frpl if year==20`y'
	replace frpl`y' = 0 if year!=20`y'
	}

gen lunch = frpl if year<=2010
replace lunch = frlmb_lag if (year>2010 & grade>=4) 	
replace lunch = frl_mb3 if (year>2010 & grade<=3) 
gen lunch_miss = (lunch==.)

forv g=0/8 {	
foreach y in 11 12 13 {
	su lunch if ( year==20`y' & grade==`g' )
	replace lunch=`r(mean)' if (lunch==. & year==20`y' & grade==`g' )
	}
	}
	
foreach y in 07 08 09 10 11 12 13 {
	gen lunch`y' = lunch if year==20`y'
	replace lunch`y' = 0 if year!=20`y'
	}

gen sample0 = (grade==0 & year>2008)
gen sample1 = (grade==1 & year>2008)
gen sample2 = (grade==2 & year>2008)
gen sample3 = (grade==3 & year>2008)
gen sample4 = (grade==4 & year>2008 & charter!=. & exit!=. & math_lag!=. & read_lag!=. & lunch!=. & math_gr_lag!=. & read_gr_lag!=. & ma_aig_lag!=. & rd_aig_lag!=.)
gen sample5 = (grade==5 & year>2008 & charter!=. & exit!=. & math_lag!=. & read_lag!=. & lunch!=. & math_gr_lag!=. & read_gr_lag!=. & ma_aig_lag!=. & rd_aig_lag!=.)
gen sample6 = (grade==6 & year>2008 & charter!=. & exit!=. & math_lag!=. & read_lag!=. & science_lag!=. & lunch!=. & math_gr_lag!=. & read_gr_lag!=. & science_gr_lag!=. & ma_aig_lag!=. & rd_aig_lag!=.)
gen sample7 = (grade==7 & year>2008 & charter!=. & exit!=. & math_lag!=. & read_lag!=. & lunch!=. & math_gr_lag!=. & read_gr_lag!=. & ma_aig_lag!=. & rd_aig_lag!=.)
gen sample8 = (grade==8 & year>2008 & charter!=. & exit!=. & math_lag!=. & read_lag!=. & lunch!=. & math_gr_lag!=. & read_gr_lag!=. & ma_aig_lag!=. & rd_aig_lag!=.)
keep if (sample0==1 | sample1==1 | sample2==1 | sample3==1 | sample4==1 | sample5==1 | sample6==1 | sample7==1 | sample8==1) 

replace appch1lead=1 if (appch1==166)
replace appch1dl = 1 if (appch1==154)
replace appch1litd = 1 if inlist(appch1 , 175 , 176 , 177 , 178 , 179 , 184)
gen appch1stem = 1 if inlist(appch1 , 180 , 181)
replace appch1magnet=1 if (appch1lead==1 | appch1dl==1 | appch1litd==1 | appch1stem==1 | appch1ib==1 | appch1arts==1 | appch1accel==1 | appch1mont==1 | appch1trad==1)
gen appch1nclb = ((appch1>=186 & appch1<=190) | (appch1>=192 & appch1<=199))
replace appch1nclb = 1 if (appch1>=244 & appch1<=255) 
replace appch1nclb = 1 if (appch1>=601 & appch1<=628)
gen appch1other = (appch1magnet==0 & appch1nclb==0)

gen type = ""
replace type = "dl" if appch1dl==1
replace type = "litd" if appch1litd==1
replace type = "mont" if appch1mont==1
replace type = "ib" if appch1ib==1
replace type = "lead" if appch1lead==1
replace type = "arts" if appch1arts==1
replace type = "trad" if appch1trad==1
replace type = "open" if appch1open==1
replace type = "accel" if appch1accel==1
replace type = "stem" if appch1stem==1
replace type = "nclb" if appch1nclb==1
replace type = "other" if appch1other==1 

gen meetreq = .
replace meetreq=mardgr_int_lag if appch1ib==1 & grade>=6
replace meetreq=0 if appch1litd==1 & inlist(grade , 3 , 4 , 5)
replace meetreq=mardgr_int_lag if appch1stem==1 & inlist(grade , 4 , 5)
replace meetreq=allgr_int_lag if appch1stem==1 & (grade==6)
replace meetreq=mardgr_int_lag if (appch1stem==1 & grade>=7) 
replace meetreq = 0 if (appch1dl==1 & grade>=2 & grade<=5) 
replace meetreq = 0 if (inlist(appch1 , 136 , 137 , 138 , 153) & grade>=1 & grade<=5)
replace meetreq=read_gr_lag if appch1dl==1 & grade>=6
replace meetreq=0 if (appch1arts==1 | appch1lead==1) & (grade>=6)
replace meetreq=1 if meetreq==.
keep if meetreq==1

egen lottery = group(grade appch1 year)
bysort lottery: egen winprob=mean(wonfirst)
keep if winprob>0 & winprob<1 
xtset lottery

gen assch_link = asschoice

merge m:1 assch_link year using asschoices.dta , gen(merge_assch)
keep if inlist(merge_assch , 1 , 3)
merge m:1 assschl using "scores-assigned.dta" , gen(merge_assscores)
keep if inlist(merge_assscores , 1 , 3)

drop if mathstand_as==.
keep if year<2013

gen madeapp=1
gen appk = grade==0
gen lostk = (wonfirst==0 & grade==0)
bysort mastid: egen appk2 = max(appk)
bysort mastid: egen lostk2 = max(lostk)

** Figure 1
collapse (count) numapps=appch1 (mean) propwon=wonfirst , by(grade)

gen grade1 = grade - 0.2
gen grade2 = grade + 0.2

label define gradelabel 0 "K" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8"
label values grade gradelabel

label var grade1 " "
label var grade2 " "

set scheme s1mono

twoway bar numapps grade1 , barw(0.4) col(gs6) lc(black) yaxis(1) || bar propwon grade2 , barw(0.4) col(gs12) lc(black) yaxis(2) , ///
	legend(label(1 "Number of Apps") label(2 "Pct Won")) xlabel(0 "K" 1 "1st" 2 "2nd" 3 "3rd" 4 "4th" 5 "5th" 6 "6th" 7 "7th" 8 "8th") ///
	ylabel(0(0.2)1 , axis(2)) ytitle("Number of Applicants in Sample" , axis(1)) ytitle("Pct. Won First Choice" , axis(2))

graph export figure1.png , replace





